﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class ccostovacacionctservController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /ccostovacacionctserv/

        public ActionResult Indexvacacion(int id)
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            var query = entidades.CCOSTOVACACIONCTSERVs
                .Where(ccvcts => ccvcts.idrecordvacacion == id);

            var result = (from ccvcts in query.OrderByDescending(ccpv => ccpv.IdCCostoVacacionCTServ).Skip(start).Take(limit).ToList()
                          join cc in entidades.CCOSTOes on ccvcts.IdCCosto equals cc.IdCCosto
                          join pya in entidades.PROYACTs on cc.IdProyAct equals pya.IdProyAct
                          join pyc in entidades.PROYCOMPs on pya.IdProycomp equals pyc.IdProyComp
                          join cpy in entidades.CONVEPROYs on pyc.IdConvProy equals cpy.IdConvProy
                          join py in entidades.PROYECTOes on cpy.IdProyecto equals py.IdProyecto
                          join eg in entidades.ELEMENTOGASTOes on cc.IdElemGasto equals eg.IdElemGasto
                          select new
                          {
                              Id = ccvcts.IdCCostoVacacionCTServ,
                              idrecordvacacion = ccvcts.idrecordvacacion,
                              CodCCosto = cc.Codigo,
                              IdProyecto = py.IdProyecto,
                              NomCCosto = eg.NomElemGasto,
                              Monto = ccvcts.valor,
                              Porcentaje = ccvcts.porcentaje,
                              Estado = ccvcts.estado
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /ccostovacacionctserv/

        public ActionResult Indexvacts(int id)
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            var query = entidades.CCOSTOVACACIONCTSERVs
                .Where(ccvcts => ccvcts.IdCTServ == id);

            var result = (from ccvcts in query.OrderByDescending(ccpv => ccpv.IdCCostoVacacionCTServ).Skip(start).Take(limit).ToList()
                          join cc in entidades.CCOSTOes on ccvcts.IdCCosto equals cc.IdCCosto
                          join pya in entidades.PROYACTs on cc.IdProyAct equals pya.IdProyAct
                          join pyc in entidades.PROYCOMPs on pya.IdProycomp equals pyc.IdProyComp
                          join cpy in entidades.CONVEPROYs on pyc.IdConvProy equals cpy.IdConvProy
                          join py in entidades.PROYECTOes on cpy.IdProyecto equals py.IdProyecto
                          join eg in entidades.ELEMENTOGASTOes on cc.IdElemGasto equals eg.IdElemGasto
                          select new
                          {
                              Id = ccvcts.IdCCostoVacacionCTServ,
                              IdCTServ = ccvcts.IdCTServ,
                              CodCCosto = cc.Codigo,
                              IdProyecto = py.IdProyecto,
                              NomCCosto = eg.NomElemGasto,
                              Monto = ccvcts.valor,
                              Porcentaje = ccvcts.porcentaje,
                              Estado = ccvcts.estado
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /ccostovacacionctserv/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ccostovacacionctserv/Create

        public ActionResult Create()
        {
            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "POR PAGAR", Value = "false" });
            siono.Add(new SelectListItem { Text = "PAGADO", Value = "true" });

            ViewData["estado"] = new SelectList(siono.AsEnumerable(), "value", "text");

            return View();
        } 

        //
        // POST: /ccostovacacionctserv/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection, CCOSTOVACACIONCTSERV CCOSTOVACACIONCTSERV)
        {
            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "POR PAGAR", Value = "false" });
            siono.Add(new SelectListItem { Text = "PAGADO", Value = "true" });

            ViewData["estado"] = new SelectList(siono.AsEnumerable(), "value", "text");

            try
            {
                // TODO: Add insert logic here
                var cccvcts = new CCOSTOVACACIONCTSERV();
                if (ModelState.IsValid)
                {
                    TryUpdateModel(cccvcts, collection.ToValueProvider());
                    cccvcts.estado = false;
                    entidades.AddToCCOSTOVACACIONCTSERVs(cccvcts);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ccostovacacionctserv/Edit/5

        public ActionResult Edit(int id)
        {
            var eccvcts = entidades.CCOSTOVACACIONCTSERVs.Single(ccvcts => ccvcts.IdCCostoVacacionCTServ == id);

            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "POR PAGAR", Value = "false" });
            siono.Add(new SelectListItem { Text = "PAGADO", Value = "true" });

            ViewData["estado"] = new SelectList(siono.AsEnumerable(), "value", "text", eccvcts.estado);

            return View(eccvcts);
        }

        //
        // POST: /ccostovacacionctserv/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection, CCOSTOVACACIONCTSERV CCOSTOVACACIONCTSERV)
        {
            var eccvcts = entidades.CCOSTOVACACIONCTSERVs.Single(ccvcts => ccvcts.IdCCostoVacacionCTServ == id);

            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "POR PAGAR", Value = "false" });
            siono.Add(new SelectListItem { Text = "PAGADO", Value = "true" });

            ViewData["estado"] = new SelectList(siono.AsEnumerable(), "value", "text");

            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    TryUpdateModel(eccvcts);
                    entidades.SaveChanges();
                    //entidades.Rvalidarcentrodecostouno(eccpersona.IdPersonal);
                    return Content("OK");
                }
                return View(eccvcts);
            }
            catch
            {
                return View(eccvcts);
            }
        }

        //
        // GET: /ccostovacacionctserv/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /ccostovacacionctserv/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
